#if defined(GD32F10x)
#include "gd32f10x.h"
#elif defined(GD32F1x0)
#include "gd32f1x0.h"
#elif defined(GD32F20x)
#include "gd32f20x.h"
#elif defined(GD32F3x0)
#include "gd32f3x0.h"
#elif defined(GD32F30x)
#include "gd32f30x.h"
#elif defined(GD32F4xx)
#include "gd32f4xx.h"
#elif defined(GD32F403)
#include "gd32f403.h"
#elif defined(GD32E10X)
#include "gd32e10x.h"
#elif defined(GD32E23x)
#include "gd32e23x.h"
#elif defined(GD32E50X)
#include "gd32e50x.h"
#else
#error "Unknown chip series"
#endif

#include "plantform_systick.h"
#include "aoi.h"

/* define blinky LED pin here, board specific, otherwise default PC13 */
#ifdef GD32350G_START
/* correct LED for GD32350G-START board. PA1 */
#define LEDPORT GPIOA
#define LEDPIN GPIO_PIN_1
#define LED_CLOCK RCU_GPIOA
#else
#define LEDPORT GPIOC
#define LEDPIN GPIO_PIN_3
#define LED_CLOCK RCU_GPIOC
#endif

#define RCU_UART RCU_USART0
#define USART USART0
#define UART_TX_RX_GPIO GPIOA
#define UART_TX_GPIO_PIN GPIO_PIN_9
#define UART_RX_GPIO_PIN GPIO_PIN_10

#define UART_TX_AF GPIO_AF_1
#define UART_RX_AF GPIO_AF_1

void systick_config(void);
void uart_initial(void);
void delay_1ms(uint32_t count);

uint8_t uart_rxb[32];
uint8_t uart_rxcnt = 0;
uint8_t uart_flag = 0;
struct LCD lcd = {0};
int main(void)
{
	systick_deinit();
	//systick_init();

	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_GPIOB);

	uart_initial();

	/* enable USART TBE interrupt */
	//usart_interrupt_enable(USART0, USART_INT_TBE);
	//usart_interrupt_enable(USART0, USART_INT_RBNE);
	usart_interrupt_enable(USART0, USART_INT_IDLE);

	/* USART interrupt configuration */
	//nvic_irq_enable(USART0_IRQn, 1, 1);

	
	
	/*lcd.io_initial = lcm_io_initial;
	lcd.test = lcm_main;
	lcd.reset = lcm_reset;
	lcd.initial = lcm_initial;
	lcd.screen[0] = lcm_clear;
	lcd.screen[1] = lcm_column_even;
	lcd.screen[2] = lcm_column_odd;
	lcd.screen[3] = lcm_row_even;
	lcd.screen[4] = lcm_row_odd;
	lcd.screen[0xf] = lcm_full;*/

	aoi_config();

	aoi_start(&lcd);
}

void uart_initial(void) {
	rcu_periph_clock_enable(RCU_USART0);

	/* set output as output */
#if defined(GD32F3x0) || defined(GD32F1x0) || defined(GD32F4xx) || defined(GD32E23x)
	gpio_mode_set(LEDPORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LEDPIN);
	gpio_output_options_set(LEDPORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LEDPIN);
#else /* valid for GD32F10x, GD32E20x, GD32F30x, GD32F403, GD32E10X */
	gpio_init(LEDPORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LEDPIN);
#endif

	gpio_af_set(UART_TX_RX_GPIO, UART_TX_AF, UART_TX_GPIO_PIN);
	gpio_af_set(UART_TX_RX_GPIO, UART_RX_AF, UART_RX_GPIO_PIN);

	gpio_mode_set(UART_TX_RX_GPIO, GPIO_MODE_AF, GPIO_PUPD_PULLUP, UART_TX_GPIO_PIN);
	gpio_output_options_set(UART_TX_RX_GPIO, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, UART_TX_GPIO_PIN);
	gpio_mode_set(UART_TX_RX_GPIO, GPIO_MODE_AF, GPIO_PUPD_PULLUP, UART_RX_GPIO_PIN);
	gpio_output_options_set(UART_TX_RX_GPIO, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, UART_RX_GPIO_PIN);

	/* USART configure 115200 8N1 */
	usart_deinit(USART);
	usart_word_length_set(USART, USART_WL_8BIT);
	usart_stop_bit_set(USART, USART_STB_1BIT);
	usart_parity_config(USART, USART_PM_NONE);
	usart_baudrate_set(USART, 9600U);
	usart_receive_config(USART, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART, USART_TRANSMIT_ENABLE);
	usart_enable(USART);
}

/*
void UART0_IRQHandler(void)
{
	//若接收到一个字节
	if (RESET != usart_flag_get(USART0, USART_FLAG_RBNE)) {
		//g_ucRecvBuf_FIFO[MODBUS_UART_PORT3][g_cxRecvCnt_FIFO[MODBUS_UART_PORT3]++] = usart_data_receive(USART0);
		uart_rxb[uart_rxcnt] = usart_data_receive(USART0);
		uart_rxcnt++;

	}
	//若接收检测到空闲帧中断
	else if (RESET != usart_flag_get(USART0, USART_INT_IDLE)) {
		usart_data_receive(USART0);
		uart_rxcnt = 0;
		uart_flag = 1;
	}
}*/

volatile static uint32_t delay;

void systick_config(void)
{
	/* setup systick timer for 1000Hz interrupts */
	if (SysTick_Config(SystemCoreClock / 1000U)) {
		/* capture error */
		while (1) {
		}
	}
	/* configure the systick handler priority */
	NVIC_SetPriority(SysTick_IRQn, 0x00U);
}

void delay_1ms(uint32_t count)
{
	delay = count;

	while (0U != delay) {
	}
}

void delay_decrement(void)
{
	if (0U != delay) {
		delay--;
	}
}

void NMI_Handler(void)
{
}

void HardFault_Handler(void)
{
	while (1)
		;
}

void MemManage_Handler(void)
{
	while (1)
		;
}

void BusFault_Handler(void)
{
	while (1)
		;
}

void UsageFault_Handler(void)
{
	while (1)
		;
}

void SVC_Handler(void)
{
}

void DebugMon_Handler(void)
{
}

void PendSV_Handler(void)
{
}

/*void SysTick_Handler(void)
{
	delay_decrement();
}*/
